﻿#region Usings
using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;

using BK.Common.Web.UI.Models;
using BK.Model;
#endregion

namespace BK.Common.Web.UI.Controllers
{
    public class CommonExpensesController : Controller
    {
        //
        // GET: /CommonExpenses/

        #region Instance Methods
        public ActionResult Edit(int? id)
        {
            CommonExpense commonExpense;
            if (id.HasValue)
            {
                BkDataContext commonDataContext = new BkDataContext();
                commonExpense = commonDataContext.CommonExpenses.Where(x => x.Id == id).First();
            }
            else
            {
                commonExpense = new CommonExpense();
            }
            return View(commonExpense);
        }

        public ActionResult Index()
        {
            BkDataContext commonDataContext = new BkDataContext();

            var commonExpensesIndexModel = new CommonExpensesIndexModel();
            commonExpensesIndexModel.Items = commonDataContext.CommonExpenses.Select(x => x);
            return View(commonExpensesIndexModel);
        }

        [HttpPost]
        public ActionResult Save(CommonExpense commonExpense)
        {
            BkDataContext commonDataContext = new BkDataContext();

            if (commonExpense.Id != 0)
            {
                commonDataContext.CommonExpenses.Attach(commonExpense);
                commonDataContext.Entry(commonExpense).State = EntityState.Modified;
            }
            else
            {
                commonDataContext.CommonExpenses.Add(commonExpense);
            }
            commonDataContext.ChangeTracker.DetectChanges();
            commonDataContext.SaveChanges();
            return View("Edit", commonExpense);
        }
        #endregion
    }
}